package ar.edu.utn.frba.tacs.warnes.rest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.restlet.resource.Get;
import org.restlet.resource.Put;

import ar.edu.utn.frba.tacs.warnes.Auto;
import ar.edu.utn.frba.tacs.warnes.aop.Utils;
import ar.edu.utn.frba.tacs.warnes.excepciones.RestException;
import ar.edu.utn.frba.tacs.warnes.utils.ContenedorWarnes;

import com.thoughtworks.xstream.XStream;

public class AutoResource {
	XStream xstream = new XStream();
	private final String dir = System.getProperties().getProperty("user.dir");

	@Get
	public String getAutoPorId(Long id) {
		Auto auto = ContenedorWarnes.getInstance().getAutos(id).get(0);
		return this.guardarAuto(auto);
	}

	@Put
	public String guardarAuto(Auto auto) {
		String xml;
		String ruta = dir + "\\rest\\auto\\";
		xml = xstream.toXML(auto);
		try {
			Utils.grabarArchivoADisco(ruta, "auto-id=" + auto.getId()
					+ ".xml", xml);
		} catch (IOException ex) {
			throw new RestException(ex.getMessage());
		}
		return xml;
	}
}

